This guide walks through the process of creating a central configuration management for microservices using Spring Cloud Config integrating with HashiCorp Vault.

Introduction

Nowadays, software is commonly delivered as a service and doesn’t matter the programming language that was chosen, it’s always good to follow the twelve-factor app methodology.

The first factor is about the codebase, it starts saying:

One codebase tracked in revision control, many deploys

It means that the same codebase needs to be deployed in multiple environments without any change other than the configuration, which brings us to the externalized configuration.

If you are working (or going to work) with microservices in an elastic environment, you probably noticed the need for a central place for configuration management, and that’s also one of the twelve-factors.

Pre-req

Spring Cloud Config

Spring Cloud Config provides server and client-side support for externalized configuration in a distributed system and it has quite few backend storage support, this guide covers the following:

Setting the Config Server

The easiest way to set up a Config Server is reaching start.spring.io, search for Config Server in the dependencies search box and hit the Generate Project button, it will generate a zip file containing the Config Server project with all the necessary dependencies. Unzip the project, open the main class and add the @EnableConfigServer on the class level.

You need to end up with something like this:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;

@EnableConfigServer
@SpringBootApplication
public class ConfigServerApplication {

  public static void main(String[] args) {
    SpringApplication.run(ConfigServerApplication.class, args);
  }

}

The generated project has an application.properties file placed at src/main/resources, for personal preferences I renamed it to application.yml.

Open the application.yml file and add the following configuration, as I’m running all the applications in the same host it’s important to change the server.port.

server:
  port: 8888

spring:
  application:
    name: configserver

Profiles & Auto of the box implementations

Spring Cloud Config Server uses profiles to provide multiple auto of the box backend storages implementations, let’s walk through some of them.

File System Backend

There’s a native profile available where the Config Server searches for the properties/YAML files from the local classpath or file system.

spring:
  profiles:
    active: native

The default value of the searchLocations is identical to a local Spring Boot application (that is, [classpath:/, classpath:/config, file:./, file:./config]). This does not expose the application.properties from the server to all clients, because any property sources present in the server are removed before being sent to the client.

You can point to any location using spring.cloud.config.server.native.searchLocations.

Remember to use the file: prefix for file resources (the default without a prefix is usually the classpath). As with any Spring Boot configuration, you can embed ${}-style environment placeholders, but remember that absolute paths in Windows require an extra / (for example, file:///${user.home}/config-repo).

For this guide I’ll create a folder named config-repo in the ${user.home}, this new folder is created to store the configuration files for the native profile.

Now add the following configuration to application.yml.

spring:
  cloud:
    config:
      server:
        native:
          searchLocations: file://${user.home}/config-repo

Now create the file config-repo/configclient.yml, this file stores the configuration for our microservice (created later in this guide) named configclient, and add the following configuration.

client:
  pseudo:
    property: Property value loaded from File System

Now you can start the application and hit the endpoint http://localhost:8888/configclient/default, the response will be:

{  
   "name":"configclient",
   "profiles":[  
      "default"
   ],
   "label":null,
   "version":null,
   "state":null,
   "propertySources":[  
      {  
         "name":"file:///Users/my-user/config-repo/configclient.yml",
         "source":{  
            "client.pseudo.property":"Property value loaded from File System"
         }
      }
   ]
}

The native profile is a good way to start with Config Server on the local environment, but I don’t recommend anyone to go to production using a file system based storage.

Git Backend

There’s also a git profile where you can point to an external git repository that contains all the configurations files for your microservices. To make it work you can either add the git profile to the active profiles or totally remove the spring.profiles.active properties. If you choose to keep this property, the last specified profile has priority and overrides all the previously defined.

Add the following config:

spring:
  profiles:
    active: native, git
  cloud:
    config:
      server:
        git:
          uri: https://github.com/weekly-drafts/config-repo-spring-cloud-configserver-vault

Here’s the full configuration for reference:

spring:
  profiles:
    active: native, git
  application:
    name: configserver
  cloud:
    config:
      server:
        native:
          searchLocations: file://${user.home}/config-repo
        git:
          uri: https://github.com/weekly-drafts/config-repo-spring-cloud-configserver-vault

Now if you hit the http://localhost:8888/configclient/default again, there will be the following result:

{  
   "name":"configclient",
   "profiles":[  
      "default"
   ],
   "label":null,
   "version":null,
   "state":null,
   "propertySources":[  
      {  
         "name":"file:///Users/my-user/config-repo/configclient.yml",
         "source":{  
            "client.pseudo.property":"Property value loaded from File System"
         }
      },
      {  
         "name":"https://github.com/weekly-drafts/config-repo-spring-cloud-configserver-vault/configclient.yml",
         "source":{  
            "client.pseudo.property":"Property value loaded from Git"
         }
      }
   ]
}

If no profile is set git is the default one.

Vault Backend

About Vault:

Vault is a tool for securely accessing secrets. A secret is anything that to which you want to tightly control access, such as API keys, passwords, certificates, and other sensitive information. Vault provides a unified interface to any secret while providing tight access control and recording a detailed audit log.

There’s also a vault profile that enables integration with Vault to securely store the application properties, in this part, we’ll be using docker to create a vault container.

Create a vault container:

docker run -d -p 8200:8200 --name vault -e 'VAULT_DEV_ROOT_TOKEN_ID=myroot' -e 'VAULT_DEV_LISTEN_ADDRESS=0.0.0.0:8200' vault

It’s specified the root token here, do not do that in production.

Enter in the vault container and authenticate to vault:

docker exec -i -t vault sh
export VAULT_ADDR='http://localhost:8200'
vault auth myroot

Now it’s time to write our secret property:

vault write secret/configclient client.pseudo.property="Property value loaded from Vault"

Now we have everything in place, it’s time to configure our Config Server to talk to Vault, add the following properties:

spring:
  profiles:
    active: vault
  cloud:
    config:
      server:
        vault:
          port: 8200
          host: 127.0.01

Complete configuration for reference:

spring:
  profiles:
    active: native, git, vault
  application:
    name: configserver
  cloud:
    config:
      server:
        native:
          searchLocations: file://${user.home}/config-repo
        git:
          uri: https://github.com/weekly-drafts/config-repo-spring-cloud-configserver-vault
        vault:
          port: 8200
          host: 127.0.01

You can also secure properties without vault using encryption.

Setting the Config Client

As before, the easiest way to create the config client is reaching start.spring.io and adding Config Client and Web dependencies. Open the generated project, add a resources/bootstrap.yml with the following content.

spring:
  application:
    name: configclient
  cloud:
    config:
      token: myroot
      uri: http://localhost:8888

It’s important to add exactly configclient as spring.application.name because that’s the name used to bind to the external configuration.

Now open the main class and add a @Value to recover our pseudo property and return it to an endpoint, here’s my main class for reference.

@RestController
@SpringBootApplication
public class ConfigClientApplication {

    public static void main(String[] args) {
        SpringApplication.run(ConfigClientApplication.class, args);
    }

    @Value("${client.pseudo.property}")
    private String pseudoProperty;

    @GetMapping("/property")
    public ResponseEntity<String> getProperty() {
        return ResponseEntity.ok(pseudoProperty);
    }
}

After starting the project, you’ll be able to reach http://localhost:8080/property and the expected result is:

Property value loaded from Vault

Summary

Congratulations! You just created a central configuration management using Spring Cloud Config and secured all your secrets with HashiCorp Vault.

Footnote